package com.chezhujie.www.controller;

import com.chezhujie.www.core.page.Pagination;
import com.chezhujie.www.domain.Czjprovince;
import com.chezhujie.www.service.CzjprovinceService;
import com.chezhujie.www.utils.ConvertObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 *   省份
 * Created by 汤伟杰 on 2017/5/23.
 */
@Controller
@RequestMapping("/czjprovince")
public class CzjprovinceController {
    @Autowired
    private CzjprovinceService czjprovinceService;

    /** 列表 **/
    @RequestMapping("/list.do")
    public String list(Integer pageNo, Integer pageSize, Czjprovince czjprovince, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjprovince,req,model);
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> params = ConvertObj.toMap(czjprovince);
            Pagination pagination = czjprovinceService.list(pageNo.intValue(), pageSize.intValue(), params, model);
            model.addAttribute("pagination", pagination);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed","获取省份列表失败!"+e.getMessage());
        }
        model.addAttribute("entity", czjprovince);
        return "czjprovince/list";
    }
    /** 保存 **/
    @RequestMapping("/save.do")
    public String save(Czjprovince czjprovince,
                      HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        try {

            if(czjprovince.getId()!=null){
                czjprovinceService.update(czjprovince);


            }else{
                czjprovinceService.insert(czjprovince);
            }
            model.addAttribute("success", "保存省份成功");
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "保存省份失败!详情："+e.getMessage());
        }
        return "redirect:list.do";
    }


    /** 详情 **/
    @RequestMapping("/detail.do")
    public String detail(@RequestParam(required = true) int id, Czjprovince czjprovince, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjprovince,req,model);
        try {
            Czjprovince entity = czjprovinceService.queryById(String.valueOf(id));
            model.addAttribute("entity", entity);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "查询省份出错!详情："+e.getMessage());
        }
        return "czjprovince/detail";
    }
    /** 新增或修改 **/
    @RequestMapping("/add.do")
    public String add(Czjprovince czjprovince, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjprovince,req,model);
        if(czjprovince.getId()!=null){
            czjprovince = czjprovinceService.queryById(String.valueOf(czjprovince.getId()));
            model.addAttribute("entity", czjprovince);
        }
        return "czjprovince/add";
    }
    /** 删除 **/
    @RequestMapping("/delete.do")
    public String delete(@RequestParam(required = true)Integer id,Czjprovince czjprovince, HttpServletRequest req, HttpServletResponse resp,
                     ModelMap model){
        try {
            czjprovinceService.deleteById(String.valueOf(czjprovince.getId()));
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", e.getMessage());
        }
        return "redirect:list.do";
    }

    private void setMsg(Czjprovince czjprovince,HttpServletRequest req, ModelMap model) {
        model.addAttribute("failed", req.getParameter("failed"));
        model.addAttribute("success", req.getParameter("success"));
        model.addAttribute("entity", czjprovince);
    }
}
